package com.onlyphoto.onlymall.mvp.view.activity

import android.content.Intent
import android.graphics.Color
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.onlyphoto.onlymall.R
import com.onlyphoto.onlymall.base.BaseActivity
import com.onlyphoto.onlymall.bean.request.GoodsCondition
import com.onlyphoto.onlymall.bean.response.GoodsBean
import com.onlyphoto.onlymall.mvp.contract.CGoodsList
import com.onlyphoto.onlymall.mvp.presenter.PGoodsListImpl
import com.onlyphoto.onlymall.mvp.view.binder.GoodsBinder
import com.onlyphoto.onlymall.mvp.view.binder.NoMoreBinder
import com.onlyphoto.onlymall.utils.NetWorkStatus
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener
import com.scwang.smartrefresh.layout.listener.OnRefreshListener
import kotlinx.android.synthetic.main.activity_goodslist.*
import me.bakumon.statuslayoutmanager.library.DefaultOnStatusChildClickListener

import me.bakumon.statuslayoutmanager.library.StatusLayoutManager
import me.drakeet.multitype.Items
import me.drakeet.multitype.MultiTypeAdapter


class GoodsListActivity : BaseActivity<PGoodsListImpl>(), CGoodsList.IVGoodsList, View.OnClickListener, OnRefreshListener, OnLoadmoreListener {

    var content: String? = ""
    var cart2Id: String? = ""
    var condition: String? = ""
    var adapter: MultiTypeAdapter? = null
    var items: Items? = null
    var layoutManager: LinearLayoutManager? = null
    var pageNum = 1

    override fun retry() {}

    override fun init() {
        content = intent.getStringExtra("content")
        cart2Id = intent.getStringExtra("cart2Id")
        condition = intent.getStringExtra("condition")
        search_content.setText(content)
        search_content.setSelection(search_content.length())

        items = Items()
        adapter = MultiTypeAdapter(items!!)
        adapter?.register(GoodsBean::class.java, GoodsBinder())
        adapter?.register(String::class.java, NoMoreBinder())
        layoutManager = LinearLayoutManager(this)
        rv_goods.adapter = adapter
        rv_goods.layoutManager = layoutManager
    }

    override fun initListener() {
        //点击事件初始化
        back.setOnClickListener(this)
        search_content.setOnClickListener(this)
        srl_goods.setOnRefreshListener(this)
        srl_goods.setOnLoadmoreListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.back ->
                finish()
            R.id.search_content -> {
                startActivity(Intent(this, SearchActivity::class.java)
                        .putExtra("content", content))
                this.overridePendingTransition(0, 0)
            }
        }
    }

    override fun loadData() {
        if (pageNum == 1) {
            srl_goods.isEnableLoadmore = true
        }
        //加载数据
        val goodsCondition = GoodsCondition()
        goodsCondition.page_num = pageNum
        if (content != null) {
            goodsCondition.search_word = content!!
        }
        if (cart2Id != null) {
            goodsCondition.cat2_id = cart2Id!!
        }
        if (condition != null) {
            goodsCondition.condition = condition!!
        }
        mPresenter?.search(goodsCondition)
    }

    override fun onRefresh(refreshlayout: RefreshLayout?) {
        pageNum = 1
        loadData()
    }

    override fun onLoadmore(refreshlayout: RefreshLayout?) {
        pageNum++
        loadData()
    }

    override fun showEmpty() {
        srl_goods.finishRefresh()
        items?.clear()
        commonStatusView!!.setContent(R.mipmap.no_details, "没找到相关宝贝～", "")
        commonStatusView!!.setType(NetWorkStatus.EMPTY)
        showCustomLayout(commonStatusView!!, R.id.ll_content)
    }

    override fun showNoMore() {
        pageNum = 1
        srl_goods.finishLoadmore()
        srl_goods.isEnableLoadmore = false
        items?.add("已经没有更多商品了~")
        adapter?.notifyDataSetChanged()
    }

    override fun updateView() {
        showSuccess()
    }

    override fun searchSuccess(list: ArrayList<GoodsBean>) {
        srl_goods.finishRefresh()
        srl_goods.finishLoadmore()
        if (pageNum == 1) {

            items?.clear()
        }
        items?.addAll(list)
        adapter?.notifyDataSetChanged()
    }

    override fun isFullScreen(): Boolean {
        //是否沉浸式， 默认非沉浸式
        return false
    }

    public override fun initCustomerStatusManager() {
        builder = StatusLayoutManager.Builder(rv_goods)
                .setDefaultLoadingText("数据加载中...")
                .setOnStatusChildClickListener(object : DefaultOnStatusChildClickListener() {
                    override fun onCustomerChildClick(view: View) {
                        super.onCustomerChildClick(view)
                        when (view.id) {
                            R.id.ll_content -> {
                                when {
                                    view.tag == NetWorkStatus.FAIL -> retry()
                                    view.tag == NetWorkStatus.ERROR -> retry()
                                    view.tag == NetWorkStatus.EMPTY -> {
                                    }
                                }
                            }
                            else -> retry()
                        }
                    }
                })
                .setDefaultLayoutsBackgroundColor(Color.WHITE)
        statusLayoutManager = builder!!.build()
    }

    override fun setContentViewId(): Int {
        return R.layout.activity_goodslist
    }

    override fun createPresenter() {
        mPresenter = PGoodsListImpl(mContext, this)
    }

    override fun onPause() {
        super.onPause()
        overridePendingTransition(0, 0)
    }

    override fun showLoading() {

    }

    override fun hideLoading() {

    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        content = intent.getStringExtra("content")
        search_content.setText(content)
        search_content.setSelection(search_content.length())
        loadData()
    }

}
